package com.example.yls.demoa;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageRequest;
import com.android.volley.toolbox.NetworkImageView;
import com.android.volley.toolbox.Volley;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private static final int GET_IMG = 1001;
    private Button btnHttp;
    private Button btnVolley;
    private Button btnLoader;
    private Button btnNet;
    private NetworkImageView networkImageView;
    private ImageView ivImg;
    private Handler handler;
    private String imgUrl1 = "http://p.ananas.chaoxing.com/star3/270_160c/53cf3d81a310abc6bf2334b3.jpg";
    private String imgUrl2 = "http://p.ananas.chaoxing.com/star3/120_120c/29c7555fcccd9cd37001b373f7a95a1d.jpg";
    private String imgUrl3 = "http://i.mooc.chaoxing.com/images/ewm.png";
    RequestQueue requestQueue;
    private MyImageCache myImageCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btnHttp = findViewById(R.id.btn1);
        ivImg = findViewById(R.id.iv_age);
        btnVolley = findViewById(R.id.btn2);
        btnLoader = findViewById(R.id.btn3);
        btnNet = findViewById(R.id.btn4);
        myImageCache = new MyImageCache();
        networkImageView = findViewById(R.id.netImg);

//       NetWorkImageView实现图片的加载
        btnNet.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageLoader imageLoader = new ImageLoader(requestQueue, myImageCache);
                networkImageView.setDefaultImageResId(R.drawable.moren);
                networkImageView.setErrorImageResId(R.drawable.error);
                networkImageView.setImageUrl(imgUrl3, imageLoader);

            }
        });

//        ImageLoader
        btnLoader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageLoader imageLoader = new ImageLoader(requestQueue, myImageCache);
                ImageLoader.ImageListener imageListener = ImageLoader.getImageListener(ivImg,
                        R.drawable.moren, R.drawable.error);
                imageLoader.get(imgUrl3, imageListener);
//                ImageLoader imageLoader = new ImageLoader(requestQueue, new ImageLoader.ImageCache() {
//                    @Override
//                    public Bitmap getBitmap(String s) {
//                        return null;
//                    }
//
//                    @Override0
//                    public void putBitmap(String s, Bitmap bitmap) {
//
//                    }
//                });

            }
        });

//       ImageRequest
        requestQueue = Volley.newRequestQueue(MainActivity.this);

        btnVolley.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ImageRequest imageRequest = new ImageRequest(imgUrl1,
                        new Response.Listener<Bitmap>() {
                            @Override
                            public void onResponse(Bitmap bitmap) {
                                ivImg.setImageBitmap(bitmap);
                            }
                        }, 0, 0, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                });
                requestQueue.add(imageRequest);
            }
        });

        handler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message message) {
                if (message.what == GET_IMG) {
                    Bitmap bitmap = (Bitmap) message.obj;
                    ivImg.setImageBitmap(bitmap);
                }
                return true;
            }
        });


//        HttpUrlConnection
        btnHttp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            URL url = new URL(imgUrl2);
                            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                            InputStream inputStream = httpURLConnection.getInputStream();
                            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

                            Message message = Message.obtain();
                            message.obj = bitmap;
                            message.what = GET_IMG;
                            handler.sendMessage(message);


                        } catch (MalformedURLException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });
    }
}
